package com.wocao.utils;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.converters.longconverter.LongStringConverter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.wocao.entity.SysUser;
import org.apache.poi.ss.formula.functions.T;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * excel工具类
 */
public class ExcelUtil<T> {

    /**
     * 导出
     *
     * @param response  HttpServletResponse
     * @param dataClass 实体类
     * @param dataList  数据集合
     * @throws IOException io
     */
    public static <T> void export(HttpServletResponse response, Class<T> dataClass, List<T> dataList) throws IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        response.addHeader("Content-Disposition", "attachment;filename=file.xlsx");

        EasyExcel.write(response.getOutputStream(), dataClass)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet("sheet1")
                .registerConverter(new LongStringConverter())
                .doWrite(dataList);
    }

}
